package com.cash.channel.svc.logic;

import com.cash.channel.svc.dao.ApplyPushFlowMapper;
import com.cash.channel.svc.model.ApplyPushFlow;
import com.alibaba.fastjson.JSON;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.request.ApplySubmitRequest;
import com.geex.haneda.service.ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ApplySubmitLogic {

    @Autowired
    private ApplyPushFlowMapper applyPushFlowMapper;

    @Autowired
    private ApiService hanedaApiService;


    public Integer insertApplyPushFlow(ApiRequestModel apiRequestModel, ApplySubmitRequest applySubmitRequest) {
        ApplyPushFlow applyPushFlow = new ApplyPushFlow();
        applyPushFlow.setcFlowId(applySubmitRequest.getOrderNo());
        applyPushFlow.setcChannel(apiRequestModel.getChannel());
        List<ApplyPushFlow> flows = applyPushFlowMapper.select(applyPushFlow);
        if (flows.isEmpty()) {
            applyPushFlow.setcContent(apiRequestModel.getData());
            applyPushFlow.setdCreate(new Date());
            applyPushFlow.setcOrderId(applySubmitRequest.getOrderNo());
            return applyPushFlowMapper.insert(applyPushFlow);
        } else {
            applyPushFlow = flows.get(0);
            applyPushFlow.setcContent(apiRequestModel.getData());
            applyPushFlow.setdCreate(new Date());
            applyPushFlow.setcOrderId(applySubmitRequest.getOrderNo());
            applyPushFlowMapper.updateByPrimaryKeySelective(applyPushFlow);
            return applyPushFlow.getnId();
        }

    }

    public void updateApplyPushFlow(Integer flowId, ApiResponseModel apiResponseModel) {
        ApplyPushFlow applyPushFlow = applyPushFlowMapper.selectByPrimaryKey(flowId);
        applyPushFlow.setdUpdate(new Date());
        applyPushFlow.setcResult(JSON.toJSONString(apiResponseModel));
        applyPushFlowMapper.updateByPrimaryKeySelective(applyPushFlow);
    }

}
